package com.wyh.service.serviceImpl.g3serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyh.common.result.Result;
import com.wyh.domain.dto.g3dto.G3BillingItemsDto;
import com.wyh.domain.po.g3po.BillingItems;
import com.wyh.domain.po.g3po.G3Contractitem;
import com.wyh.domain.po.g3po.G3Formula;
import com.wyh.domain.po.g3po.UnitPrice;
import com.wyh.mapper.g3mapper.BillingAssociationTableMapper;
import com.wyh.mapper.g3mapper.G3BillingItemsMapper;
import com.wyh.mapper.g3mapper.UnitPriceMapper;
import com.wyh.service.g3service.BillingItemsService;
import com.wyh.service.g3service.ContractItemService;
import com.wyh.service.g3service.FormulaService;
import com.wyh.utils.g3utils.BillingItemsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

import static com.wyh.utils.g3utils.BillingItemsUtils.StringToList;

@Service
public class BillingItemsServiceImpl extends ServiceImpl<G3BillingItemsMapper, BillingItems> implements BillingItemsService {

    @Autowired
    G3BillingItemsMapper g3BillingItemsMapper;

    @Autowired
    BillingItemsService billingItemsService;

    @Autowired
    FormulaService formulaService;

    @Autowired
    BillingAssociationTableMapper billingAssociationTableMapper;

    @Autowired
    ContractItemService contractItemService;

    @Autowired
    UnitPriceMapper unitPriceMapper;


    @Override
    public BillingItems getBillingItemsAndPriceById(Integer id) {
        return g3BillingItemsMapper.getBillingItemsAndPriceById(id);
    }

    //通过billingitems中的formula字符串得到计费公式
    @Override
    public List<G3Formula> getFormulas(String formula) {
        List<Integer> integers = BillingItemsUtils.StringToList(formula);
        return formulaService.listByIds(integers);
    }

    @Override
    public BillingItems getPeriodicbillingAndThreeMeterbillingById(Integer id) {
        return g3BillingItemsMapper.getPeriodicbillingAndThreeMeterbillingById(id);
    }

    //    根据费项id修改费项名称 合同费项表
    @Override
    public boolean updateName(Integer item, String name) {
        if (item != null && name != null) {
            QueryWrapper<G3Contractitem> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("item", item);
            List<G3Contractitem> list = contractItemService.list(queryWrapper);
            if (list.isEmpty()){//判断从合同费项表从查询的该费项id使用列表,
                return true;
            }else {
                for (G3Contractitem g3Contractitem : list) {
                    if (g3Contractitem.getName().equals(name)){
                        return true;
                    }else {
                        g3Contractitem.setName(name);
                    }
                }
                boolean b = contractItemService.updateBatchById(list);
                return b;
            }

        } else {
            return false;
        }


    }

    @Override
    public boolean updateFormula(BillingItems billingItems1, Integer fid) {
        if (billingItems1.getFormula()!=null){
            String removeAndJoinNumbers = BillingItemsUtils.findRemoveAndJoinNumbers(StringToList(billingItems1.getFormula()), fid);
            billingItems1.setFormula(removeAndJoinNumbers);
            int i = g3BillingItemsMapper.updateById(billingItems1);
            if (i > 0) {
                return true;
            } else {
                return false;
            }
        }else {
            return true;
        }
    }

    //修改费项
    @Override
    public boolean updateBillingItem(G3BillingItemsDto g3BillingItemsDto) {
        if (g3BillingItemsDto != null) {

            BillingItems billingItems = g3BillingItemsMapper.selectById(g3BillingItemsDto.getId());






            if (billingItems==null){
                return false;
            }
            BillingItems billingItems1 = BillingItemsUtils.getBillingItems(g3BillingItemsDto);
            if (billingItems.equals(billingItems1)){
                return true;
            }
            //修改formal
            if (!g3BillingItemsDto.getG3Formulas().isEmpty()){
                StringBuilder idsStringBuilder = new StringBuilder();
                for (G3Formula g3Formula : g3BillingItemsDto.getG3Formulas()) {
                    Integer id = g3Formula.getId();
                    if (idsStringBuilder.length() > 0) {
                        idsStringBuilder.append(","); // 在非首个元素前添加逗号
                    }
                    idsStringBuilder.append(id);
                }

                String idsString = idsStringBuilder.toString();
                billingItems1.setFormula(idsString);
            }
            int type = billingItems.getType();//得到计费性质
            billingItems1.setType(type);//计费性质不变
            if (type == 4) {//计费性质等于4表示预存计费，预存账户类型不变
                billingItems1.setSpecialName(billingItems.getSpecialName());//预存账户类型不变
            }
            int i = g3BillingItemsMapper.updateById(billingItems1);

            if (i>0){
                return true;
            }else {
                return false;
            }
        } else {
            return false;
        }

    }


    //修改多个单价
    @Override
    public boolean updateUnitPrices(G3BillingItemsDto g3BillingItemsDto) {
        if (g3BillingItemsDto != null) {
            for (UnitPrice unitPrice : g3BillingItemsDto.getUnitPrices()) {
                unitPrice.setBillid(g3BillingItemsDto.getId());
                if (unitPrice.getId() != null) {//修改的单价
                    int i1 = unitPriceMapper.updateById(unitPrice);
                    if (i1 == 0) {
                        return false;
                    }
                } else {//新增的单价
                    unitPrice.setPrice(unitPrice.getPrice());
                    unitPrice.setUpdatetime(new Timestamp(System.currentTimeMillis()));
                    unitPrice.setUserid(g3BillingItemsDto.getUserid());
                    int insert = unitPriceMapper.insert(unitPrice);
                    if (insert == 0) {
                        return false;
                    }
                }
            }
            return true;
        } else {
            return true;
        }

    }

    @Override
    public int getParentOrganization(int id) {
        int currentOrgId = id;
        while (true) {
            int parentById = g3BillingItemsMapper.getParentById(currentOrgId);
            if (parentById == 0) {
                return currentOrgId; // 当前节点就是顶层节点
            }
            currentOrgId = parentById; // 继续向上查找
        }
    }


}
